using System;
using System.Collections.Generic;
using System.Linq;

namespace Pinwheel.LSerialize
{
    public static partial class LSerializeExtension
    {
        public static bool IsContentEqual<T>(this T[] v1, T[] v2)
        {
            if (ReferenceEquals(v1, v2))
            {
                return true;
            }
            
            if (null == v1 || null == v2 || v1.Length != v2.Length)
            {
                return false;
            }
            
            for (int i = 0; i < v1.Length; i++)
            {
                if (!v1[i].Equals(v2[i]))
                {
                    return false;
                }
            }
            return true;
        }
        
        public static int GetContentHash<T>(this T[] v)
        {
            if (null == v)
            {
                return 0;
            }
            
            int hash = 0;
            for (int i = 0; i < v.Length; i++)
            {
                hash = hash * 31 + v[i].GetHashCode();
            }
            return hash;
        }
        
        public static bool IsContentEqual<T>(this List<T> v1, List<T> v2)
        {
            if (ReferenceEquals(v1, v2))
            {
                return true;
            }
            
            if (null == v1 || null == v2 || v1.Count != v2.Count)
            {
                return false;
            }
            
            for (int i = 0; i < v1.Count; i++)
            {
                if (!v1[i].Equals(v2[i]))
                {
                    return false;
                }
            }
            return true;
        }
        
        public static int GetContentHash<T>(this List<T> v)
        {
            if (null == v)
            {
                return 0;
            }
            
            int hash = 0;
            for (int i = 0; i < v.Count; i++)
            {
                hash = hash * 31 + v[i].GetHashCode();
            }
            return hash;
        }
    }
}
